/*
 * Timer_delay.cpp
 *
 *  Created on: Jul 16, 2016
 *      Author: Yang
 */

#include "Timer_delay.hpp"

TIM_HandleTypeDef htim13;

void Timer_delay::init(void)
{
    __HAL_RCC_TIM13_CLK_ENABLE();

    htim13.Instance = TIM13;

    htim13.Init.Period = 10000 - 1;
    htim13.Init.Prescaler = 84 - 1;
    htim13.Init.ClockDivision = 0;
    htim13.Init.CounterMode = TIM_COUNTERMODE_UP;
    HAL_TIM_Base_Init(&htim13);
    HAL_TIM_Base_Stop(&htim13);
}

void Timer_delay::delay_us(uint32_t time)
{
    TIM13->CNT = 0;
    HAL_TIM_Base_Start(&htim13);
    for (;;)
    {
        if (TIM13->CNT >= time)
        {
            HAL_TIM_Base_Stop(&htim13);
            break;
        }
    }
}

void Timer_delay::delay_ms(uint32_t time)
{
    uint16_t i;
    for (i = 0; i < time; i++)
    {
        delay_us(1000);
    }
}

